package cn.fl.custpay.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.custpay.dto.PayInsRcvAccDTO;
import cn.fl.custpay.service.IPayInsRcvAccApiService;
import cn.fl.custpay.service.IPayInsRcvAccService;
import cn.fl.custpay.transform.PayInsRcvAccVOTransform;
import cn.fl.custpay.vo.PayInsRcvAccVO;
import fl.constant.facade.framework.constant.DictionaryConstant;
import fl.ref.fncprjbscinfo.condition.FncPrjBscInfoQueryCondition;
import fl.ref.fncprjbscinfo.dto.FncPrjBscInfoDTO;
import fl.ref.fncprjbscinfo.service.IFncPrjBscInfoApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;

/**
 * @Description: 融资机构资管收款账户三方支付商户号表（客户还款） 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PayInsRcvAccServiceImpl implements IPayInsRcvAccService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IPayInsRcvAccService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;
	/**
	 * 资管服务
	 */
	@Autowired
	private IPayInsRcvAccApiService payInsRcvAccApiService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private IFncPrjBscInfoApiService fncPrjBscInfoApiService;

	@Override
	public PayInsRcvAccVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		PayInsRcvAccDTO dto = this.payInsRcvAccApiService.selectById(id);
		return PayInsRcvAccVOTransform.toVO(dto);
	}

	@Override
	public Long add(PayInsRcvAccVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.payInsRcvAccApiService.add(PayInsRcvAccVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.payInsRcvAccApiService.removeById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(PayInsRcvAccVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.payInsRcvAccApiService.modify(PayInsRcvAccVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		Pager pager = this.payInsRcvAccApiService.searchListPage(page);
		List<PayInsRcvAccVO> list = PayInsRcvAccVOTransform.toVOList((List<PayInsRcvAccDTO>) pager.getResultList());
		//翻译一下、支付平台
		String payChlCd = PropertiesCacheUtils.getProperty(DictionaryConstant.PAY_CHL_CD);
		List<KeyValueVO> listDic = this.sysDictionaryApiService.getKvList(payChlCd, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
		Map<String, String> kv = new HashMap<>();
		if (CollectionUtils.isNotEmpty(listDic)) {
			for (KeyValueVO keyValueVO : listDic) {
				kv.put(keyValueVO.getKey(), keyValueVO.getValue());
			}
		}
		if (CollectionUtils.isNotEmpty(list)) {
			for (PayInsRcvAccVO pay : list) {
				pay.setPayChlCdNm(kv.get(String.valueOf(pay.getPayChlCd())));
			}
		}
		kv.clear();
		//翻译项目名称
		List<FncPrjBscInfoDTO> fncPrjBscInfoDTOList = this.fncPrjBscInfoApiService.searchList(new FncPrjBscInfoQueryCondition());
		if (CollectionUtils.isNotEmpty(fncPrjBscInfoDTOList)) {
			for (FncPrjBscInfoDTO fncPrjBscInfoDTO : fncPrjBscInfoDTOList) {
				kv.put(String.valueOf(fncPrjBscInfoDTO.getId()), fncPrjBscInfoDTO.getFncprjnm());
			}
		}
		if (CollectionUtils.isNotEmpty(list)) {
			for (PayInsRcvAccVO pay : list) {
				pay.setPrjNm(kv.get(String.valueOf(pay.getPrjId())));
			}
		}
		kv.clear();
		//交易类型
		String transTypCd = PropertiesCacheUtils.getProperty(DictionaryConstant.TRANS_TYP_CD);
		List<KeyValueVO> transTypCdlistDic = this.sysDictionaryApiService.getKvList(transTypCd, CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());
		for (KeyValueVO keyValueVO : transTypCdlistDic) {
			kv.put(keyValueVO.getKey(), keyValueVO.getValue());
		}
		if (CollectionUtils.isNotEmpty(list)) {
			for (PayInsRcvAccVO pay : list) {
				pay.setTransTypCdNm(kv.get(pay.getTransTypCd()));
			}
		}
		pager.setResultList(list);
		return pager;
	}
}
